package cn.supuwoerc.jdbc;

import cn.supuwoerc.domain.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
public class XmlConfigJdbcTemplate {
    @Autowired
    @Qualifier("jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    @Test
    public void updateData() {
        jdbcTemplate.update("update account set money=? where name = ?", 1500, "1");
    }
    @Test
    public void deleteData() {
        jdbcTemplate.update("delete from account where name = ?", "hahah");
    }
    @Test
    public void queryData() {
        List<Account> accountList = jdbcTemplate.query("select * from account;", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(accountList);
    }
    @Test
    public void queryDataById() {
        Account account = jdbcTemplate.queryForObject("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),"2");
        System.out.println(account);
    }
    @Test
    public void queryDataCount() {
        Integer integer = jdbcTemplate.queryForObject("select count(*) from account;", Integer.class);
        System.out.println(integer);
    }
}
